package com.study.concurrentprogramming.genericiy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @Description : 参数化类型测试
 * @Author : guxuemin
 * @Version : V1.0.0
 * @Date : 2021/8/14 20:00
 */
@Slf4j
public class ParameterizedTypeTest {
    private Map<String, ParameterizedTypeTest> map;
    private Set<String> setStr;
    private Class<?> clz;
    private Holder<String> holder;
    private List<String> listStr;
    private ArrayList<String> arrayList;
    private Map.Entry<String, String> entry;

    // 非参数化类型
    private String str;
    private Integer i;
    private Set set;
    private List list;

    static class Holder<V> {
    }

    public static void main(String[] args) {
        try {
            // 拿到所有的字段
            Field[] fields = ParameterizedTypeTest.class.getDeclaredFields();
            Arrays.stream(fields).forEach(item -> {
                if (item.getGenericType() instanceof ParameterizedType) {
                    item.setAccessible(true);
                    ParameterizedType parameterizedType = (ParameterizedType) item.getGenericType();
                    log.info("{} :", item.getName());
                    log.info("getActualTypeArguments: {}", Arrays.asList(parameterizedType.getActualTypeArguments()));
                    log.info("getRawType: {}", parameterizedType.getRawType());
                    log.info("getOwnerType: {}", parameterizedType.getOwnerType());
                } else {
                    log.info("{} is not ParameterizedType", item.getName());
                }
            });
        } catch (Exception e) {
            log.info("exception:", e);
        }
    }
}
